import Taro,{ useRouter } from "@tarojs/taro";
import { View,Image,Text, } from "@tarojs/components";
import { useEffect,useState } from "react";
import {AtIcon} from 'taro-ui'
import {getCompanySpecialInfo,getCompanyCommonInfo,addFavoriteCompany} from '../../api/mode'
import {PostCard} from '../../components'
import bigHouse from '../../images/bigHouse.png'
import './index.scss'



export default function CompanyDetail(){

  const router=useRouter()
  const companyId=router.params.companyId
  const isSpecial=router.params.isSpecial
  const [companyDetailInfo,setCompanycompanyDetailInfo]=useState({})
  const [positionList,setPositionList]=useState([])
  const [isCollected,setIsCollected]=useState()
  const openid=Taro.getStorageSync(`openid`)
  const submitCompanyItem={
    openid:openid,
    companyId:companyId,
    positionId:0
  }  
  // const chooseCompany=(id)=>{
  //   switch(id){
  //     case "0": return('国企')
  //     case "1": return('央企')
  //     case "2": return('事业单位')
  //     case "3": return('政府机关')
  //     case "4": return('民营企业')
  
  //   }
  // }

  useEffect(()=>{
    console.log(router.params.companyId);
    const params={
      companyId:companyId,
      openid:Taro.getStorageSync('openid')
    }
//是否是专场招聘

    isSpecial==1
    ?
    getCompanySpecialInfo(params).then(
      res=>{
        console.log(res.data.data,"speical");
        setCompanycompanyDetailInfo(res.data.data)
        setPositionList(res.data.data.positionList)
        setIsCollected(res.data.data.collected)
      }
    )
    :
    getCompanyCommonInfo(params).then(
      res=>{
        console.log(res.data.data,"common");
        setCompanycompanyDetailInfo(res.data.data)
        setPositionList(res.data.data.positionList)
        setIsCollected(res.data.data.collected)
      }
    )  
  }
  ,[])
  console.log(positionList);

  const submitMyFavoriteCompany=()=>{
    addFavoriteCompany(submitCompanyItem).then(res=>{
      console.log('收藏成功',res);
      setIsCollected(!isCollected)//收藏图标切换
      // setFavoriteIsOpen(!favoriteIsOpen)
      if(!isCollected){
        Taro.showToast({
          title:'收藏成功',
          icon:'success',
          duration:1000
        })
      }
    })
  }
  return(
    <View>
    <View className='headCard'>

      <View className=' at-row'>
      <View className='companyName at-col at-col-10'>
      {/* <AtIcon value='home' size='20' color='#000'></AtIcon> */}
      <View className='imgBox'> <Image className='bigHouse' src={bigHouse}></Image></View>
        {companyDetailInfo.companyName}
      </View>

      {/* 收藏按钮 */}
      <AtIcon  value={isCollected?'star-2':'star'} size='30' color='#4C80AE' onClick={submitMyFavoriteCompany}></AtIcon>
      </View>


      <View className='at-row'>

        <View className='companyDetailAddressAndType at-col at-col-4'>
        <View className='companyAddres'>
              {companyDetailInfo.address}
        </View>
        <View className='companyDetailType'>
        {companyDetailInfo.type}
        </View>

        <View className='companyDetailFeature at-row at-row--wrap'>
          {companyDetailInfo.feature? companyDetailInfo.feature.map((text,index)=>(<View className='companydetail_feature_item '  key={index}><Text >{text}</Text></View>)):null}
          </View>
        </View>

          <Image src={'data:image/png;base64,'+companyDetailInfo.photo}  className='at-col at-col-6 companyDetailImage'>
          </Image>


      </View>
    </View>

    <View className='centerCard'>
      <View className='centerCardTitle'>
        公司信息
      </View>
     
      <View className='centerContent'>
        <Text className='title'>简介 : </Text>
        <View className='companyDetailIntro'> {companyDetailInfo.companyInfo}</View>
      </View>

      <View className='centerContent'>
        <Text className='title'>地址 : </Text>
        <View className='companyDetailAddress'>{companyDetailInfo.detailAddress}</View>
      </View>
    </View>

    <View className='companyPosition'>
    {positionList?positionList.map((Info,index)=>(<PostCard canJump key={index} Info={Info}></PostCard>)):null}
    </View>
</View>
  )
}